{% extends "base.html" %}

{% block title %}消息模板管理{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4 class="card-title mb-0">消息模板管理</h4>
                    <div>
                        <a href="{{ url_for('message.create_template') }}" class="btn btn-primary">
                            <i class="fas fa-plus"></i> 创建模板
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>模板名称</th>
                                    <th>消息类型</th>
                                    <th>优先级</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for template in templates %}
                                <tr>
                                    <td>{{ template.name }}</td>
                                    <td>{{ template.message_type }}</td>
                                    <td>
                                        <span class="badge badge-{{ 
                                            'danger' if template.priority == '紧急' 
                                            else 'warning' if template.priority == '高' 
                                            else 'info' if template.priority == '普通' 
                                            else 'secondary' 
                                        }}">
                                            {{ template.priority }}
                                        </span>
                                    </td>
                                    <td>{{ template.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                                    <td>
                                        <a href="{{ url_for('message.edit_template', template_id=template.id) }}" 
                                           class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-edit"></i> 编辑
                                        </a>
                                        <button class="btn btn-sm btn-outline-danger delete-template" 
                                                data-template-id="{{ template.id }}" data-template-name="{{ template.name }}">
                                            <i class="fas fa-trash"></i> 删除
                                        </button>
                                    </td>
                                </tr>
                                {% else %}
                                <tr>
                                    <td colspan="5" class="text-center">暂无消息模板</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
$(document).ready(function() {
    // 删除模板
    $('.delete-template').click(function() {
        var templateId = $(this).data('template-id');
        var templateName = $(this).data('template-name');
        
        if (confirm('确定要删除模板 "' + templateName + '" 吗？此操作不可恢复。')) {
            $.ajax({
                url: '{{ url_for("message.delete_template", template_id=0) }}'.replace('0', templateId),
                type: 'POST',
                success: function(response) {
                    if (response.success) {
                        location.reload();
                    } else {
                        alert('删除失败: ' + response.message);
                    }
                },
                error: function() {
                    alert('删除失败，请稍后重试');
                }
            });
        }
    });
});
</script>
{% endblock %}